<?php

namespace app\admin\controller;

use app\admin\model\User;
use app\common\controller\Backend;

/**
 * 平台流水管理
 *
 * @icon fa fa-circle-o
 */
class PlatformLog extends Backend
{
    protected $relationSearch = true;
    /**
     * PlatformLog模型对象
     * @var \app\admin\model\PlatformLog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\PlatformLog;
        $typeList = \app\admin\model\PlatformLog::fetchType();
        $this->view->assign('typeList', $typeList);
        $this->assignconfig('typeList', $typeList);
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看.
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            [$where, $sort, $order, $offset, $limit] = $this->buildparams();
            $total = $this->model
                 ->withJoin(['user', 'order'])
                ->where($where)
                ->where('platform_log.is_delete', 1)
                ->order($sort, $order)
                ->count();
            $list  = $this->model
                 ->withJoin(['user', 'order'])
                ->where($where)
                ->where('platform_log.is_delete', 1)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = ['total' => $total, 'rows' => $list];

            return json($result);
        }

        return $this->view->fetch();
    }

    /**
     * 详情
     */
    public function detail($ids = null)
    {
        $row = $this->model
            ->with(['users'])
            ->field('out_trade_no,user_id,start_time,end_time,days,price,rental_money,deposit,all_money,status,deposit_status,send_type,car_image,car_name,shop_name,shop_logo,shop_address,driver_name,driver_number,address_name,address_phone,address_street,createtime,transaction_id,time_end,total_fee,refund_id,refund_time,refund_money')
            ->find($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row = $row->toArray();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if($row){
            $row['status'] = $row['status_text'];
            $row['send_type'] = $row['send_type_text'];
            $row['createtime'] = $row['createtime_text'];
            $row['start_time'] = $row['start_time_text'];
            $row['end_time'] = $row['end_time_text'];
            $row['send_type'] = $row['send_type_text'];
            $row['time_end'] = $row['time_end_text'];
            if($row['deposit'] > 0){
                $row['deposit_status'] = $row['deposit_status_text'];
            }else{
                unset($row['deposit'],$row['deposit_status']);
            }
            if($row['status'] == 4){
                $row['refund_time'] = $row['refund_time_text'];
            }else{
                unset($row['refund_time'],$row['refund_money'],$row['refund_id']);
            }

            unset($row['send_type'],$row['createtime_text'],$row['start_time_text'],$row['end_time_text'],$row['send_type_text'],$row['time_end_text'],$row['status_text'],$row['deposit_status_text'],$row['refund_time_text'],$row['user_id']);
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        $row                 = $this->model->get($ids);
        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->model->where('id', 'in', $ids)->update(['is_delete' => 2, 'deletetime' => time()]);
        $this->success();
    }
}
